package com.dp.FactoryPattern;

/**
 * Created by Lzz on 3/16/2018.
 */

public class FactoryShopping {

    /**
     * 选择创建对象  --》 买什么的动作
     * @param tag
     * @return
     */
    public Shopping getBuyActon(String tag){
        Shopping shopping = null;
        if (tag.equalsIgnoreCase("jeans")) {
            shopping = new BuyJeans();
        }else if(tag.equalsIgnoreCase("tshirt")){
            shopping = new BuyTshirt();
        }else if(tag.equalsIgnoreCase("shoes")){
            shopping = new BuyShoes();
        }
        return shopping;
    }


    /**
     * 泛型1     ？extends shopping   :通配符 指定传入的是shopping的子类
     * @param clazz
     * @return
     */
    public Shopping getBuyActonGenericity(Class<? extends Shopping> clazz){
        Shopping shopping = null;
        try {
            shopping = (Shopping) Class.forName(clazz.getName()).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return shopping;
    }



    public <T>T getBuyActonGenericity02(Class<? extends T> clazz){
        T t = null;
        try {
            t = (T) Class.forName(clazz.getName()).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return t;
    }



}
